#coding:utf-8
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.bootstrap import Bootstrap
from flask.ext.login import LoginManager
from flask_wtf.csrf import CsrfProtect
from flask_moment import Moment
from config import Config
db = SQLAlchemy()
bootstrap = Bootstrap()
moment = Moment()
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'auth.login'

def create_app():
    app = Flask(__name__)
    app.config.from_object(Config)
    Config.init_app(app)
    csrf = CsrfProtect(app)

    db.init_app(app)
    bootstrap.init_app(app)
    moment.init_app(app)
    login_manager.init_app(app)

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    from .admin import admin as admin_blueprint
    app.register_blueprint(admin_blueprint, url_prefix='/admin')

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')
    
    #创建一个上传文件的蓝图
    from .fileupload import upload as upload_blueprint
    app.register_blueprint(upload_blueprint, url_prefix='/upload')
    #创建一个ueditor的蓝图
    from .ueditor import ueditor as ueditor_blueprint
    app.register_blueprint(ueditor_blueprint, url_prefix='/ueditor')
   
    #将admin之内的视图去掉csrf防护，之后可以单独将ueditor放到一个blueprint中，去掉csrf防护 
    csrf.exempt(ueditor_blueprint)
    csrf.exempt(upload_blueprint)

    return app

